package com.kgame5.xtank3d.model.cart.fsms.move {
import kgame5.enginex.Entity;
import kgame5.k3d.core.obj3d.hardvo.HardVO;
import kgame5.kgu.ai.PathFollower;
import kgame5.kgu.ai.lianlian.LianLian;
import kgame5.kutil.T;

import com.kgame5.xtank3d.GameEnum;
import com.kgame5.xtank3d.C;
import com.kgame5.xtank3d.model.tank.fsms.TankState;
import com.kgame5.xtank3d.vo.MyTankVO;

public class HuntMovState extends TankState{
	
	//__________________Propetty__________________
	
	private var pathFollower:PathFollower;
	
	private var lianlian:LianLian;//寻路径
	
	//______________________Method___________________
	public function HuntMovState(vo :MyTankVO){
		super(GameEnum.STATE_EM_HUNT_MOVE, vo);
		pathFollower=new PathFollower(vo.hardVO, C.CELLSIZE);
		lianlian=new LianLian();
	}
	
	//进入状态
	 override public function enter(info:Object=null):void{
		//定位一个目标，实施追踪.检测是否能到达目标
		var aimVO:MyTankVO=selectOpponent();
		
		//填充这个很重要，为寻路径而使用
		this.spacialManager.gc.fillColAndRow(tankVO.hardVO);
		 
		if(aimVO==null){
			//T.p("玩家都死光了);
			 fsm.SwitchState(GameEnum.STATE_EM_THINK_MOVE, null);
			 return;
		}
		
		var path:Array=findPath(hardVO, aimVO.hardVO);
		if(path==null){
			//T.p("路径不能到达");
			 fsm.SwitchState(GameEnum.STATE_EM_THINK_MOVE, null);
			 return;
		}
		
		//路径器初始化
		pathFollower.init(path, tankVO.body.HUNT_MOV);
		
		 //定时
		 //timer.start(MathDG.random(ai.hunt_dur_min, ai.hunt_dur_max));
	}

	//执行状态
	override  public function update():void{
		
		 var isOK:Boolean=pathFollower.update();
		 
		 if(isOK){
			 fsm.SwitchState(GameEnum.STATE_EM_THINK_MOVE, null);
		 }
	}
	
	//退出状态
	override  public function  exit():void{
		
	}
	
	 //找到一个敌人做对手
	 private function  selectOpponent():MyTankVO{
		for each(var ent:Entity in senceWorld.getEntityListByCateID(GameEnum.CATEID_TANK)){
			if(ent._vo.teamID != tankVO.teamID) {
				
				return ent._vo as MyTankVO;
			}
		}
		return null;
	 }
	
	//计算从一个hardVO到另一个HardVO的路径
	private function findPath(src:HardVO ,aim:HardVO):Array{
		//T.p("----------findPath",src._col,src._row,aim._col,aim._row);
		var arr:Array= lianlian.CanKill({col:src._col, row:src._row}, {col:aim._col, row:aim._row}, spacialManager.gc.getMapData());
		//T.TraceArray2(arr);
		return arr;
	}
}//end of class
}